﻿using DocumentFormat.OpenXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DOW = DocumentFormat.OpenXml.Wordprocessing;

namespace Tatibah
{
#pragma warning disable 1591
    public enum enumPageSizes
    {
        A0,
        A1,
        A10,
        A2,
        A3,
        A4,
        A5,
        A6,
        A7,
        A8,
        A9,
        Custom,
        GovLetter,
        JuniorLegal,
        Ledger,
        Letter,
        Tabloid
    }

    public enum enumPageOrientation
    {
        Portrait = DOW.PageOrientationValues.Portrait,
        Landscape = DOW.PageOrientationValues.Landscape
    }
#pragma warning restore 1591

    /// <summary>
    /// Basic physical page sizes used by Section and the Document Body
    /// </summary>
    public class BaseSectionFormatting
    {
        const int WIDTH = 0;
        const int HEIGHT = 1;

        // Default Page size is A4
        /// <summary>
        /// Units are DXA
        /// </summary>
        public UInt32Value PageWidth = 11907; //  21.0cm

        /// <summary>
        /// Units are DXA
        /// </summary>
        public UInt32Value PageHeight = 16839; // 29.7cm

        /// <summary>
        /// Portrait or Landscape
        /// </summary>
        private  enumPageOrientation pageOrientation = enumPageOrientation.Portrait;

        /// <summary>
        /// Units are DXA
        /// </summary>
        public uint TopMargin = 1134; // 2.0cm

        /// <summary>
        /// Units are DXA
        /// </summary>
        public uint BottomMargin = (1134);

        /// <summary>
        /// Units are DXA
        /// </summary>
        public uint LeftMargin = (1134);

        /// <summary>
        /// Units are DXA
        /// </summary>
        public uint RightMargin = (1134);

        /// <summary>
        /// Units are DXA
        /// </summary>
        public uint HeaderMargin = (709);  // 1.25 cm

        /// <summary>
        /// Units are DXA
        /// </summary>
        public uint FooterMargin = (709);

        private enumPageSizes pageSize = enumPageSizes.A4;

        private uint[][] pageSizes = new uint[][]
        {
            new uint [] {47685, 67416},  // A0
            new uint [] {33680, 47685},  // A1
            new uint [] {1474, 33680},   // A10
            new uint [] {23814, 1474},   // A2
            new uint [] {16840, 23814},  // A3
            new uint [] {11907, 16840},  // A4
            new uint [] {8420, 11907},   // A5
            new uint [] {5954, 8420},    // A6
            new uint [] {4196, 5954},    // A7
            new uint [] {2948, 4196},    // A8
            new uint [] {2098, 2948},    // A9
            new uint [] {0, 0},          // Custom
            new uint [] {11521, 15122},  // GovLetter
            new uint [] {11521, 7201},   // JuniorLegal
            new uint [] {24494, 15819},  // Ledger
            new uint [] {12242, 15842},  // Letter
            new uint [] {15819, 24494},  // Tabloid
        };

        /// <summary>
        /// Common page size
        /// </summary>
        public enumPageSizes CurrentPageSize
        {
            get { return pageSize; }
           
        }

        /// <summary>
        /// Portrait or Landscape
        /// </summary>
        public  enumPageOrientation PageOrientation
        {
            get { return pageOrientation; }
            set
            {
                if (value == pageOrientation) return; // Nothing changed
                // Swap height and width
                uint temp = PageHeight;
                PageHeight = PageWidth;
                PageWidth = temp;
                pageOrientation = value;
            }
        }

        /// <summary>
        /// Set the page size to some well known sizes and optinally set the page orientation
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="Orientation"></param>
        public void SetPageSize(enumPageSizes PageSize, enumPageOrientation Orientation = enumPageOrientation.Portrait)
        {
            
            // If new size is Custom we don't change the page width or height
            if (PageSize != enumPageSizes.Custom)
            {
                uint[] size = pageSizes[(int)PageSize];

                if (Orientation == enumPageOrientation.Portrait)
                {
                    PageWidth = size[WIDTH];
                    PageHeight = size[HEIGHT];
                }
                else
                {
                    PageWidth = size[HEIGHT];
                    PageHeight = size[WIDTH];
                }
                pageOrientation = Orientation;
            }
            pageSize = PageSize;
        }
    }
}